package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


import sjtu.ist.warehouse4ws.model.Role;
import sjtu.ist.warehouse4ws.model.Rolepermissions;





public class ParserRole {
	
	public String parseNode(String tagName, Document doc){
		
		  NodeList parentlist= doc.getElementsByTagName(tagName);      //获取该ID的标签
		  Element headnode= (Element) parentlist.item(0);                 //获取该ID标签的第一个Item
		  if(headnode==null){return null;}
	      NodeList childlist=headnode.getChildNodes();                 //获取该ID标签第一个Item里面的结点
			if (childlist.getLength() != 0) {
				String tagvalue = childlist.item(0).getNodeValue(); // 取值
				if (tagvalue.isEmpty()) {
					return null;
				} else {
					return tagvalue;
				}
			} else {
				return null;
			}
	}
	public Role parse(String rootPath) throws SAXException, IOException, ParserConfigurationException   {
	    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	    
	    try{
		 	DocumentBuilder db = dbf.newDocumentBuilder();
		 	
		 	
		 	//Change2UTF.change(rootPath);  //change other coded file to UTF-8
		 	File file = new File(rootPath);
           
            Document doc = db.parse(file); 
            String id=parseNode("id",doc);
            String enName=parseNode("enName",doc);
            String cnName=parseNode("cnName",doc);
            String description=parseNode("description",doc);
            String rolePackage=parseNode("rolePackage",doc);
            String roleType=parseNode("roleType",doc);
            String parentRoleId=parseNode("parentRoleId",doc);
            String departmentid=parseNode("departmentid",doc);
            
	      
	        Role role=new Role();        //the start point of the process
            role.setId(id);
            role.setEnName(enName);
            role.setCnName(cnName);
            role.setDescription(description);
            role.setRolePackage(rolePackage);
            role.setRoleType(roleType);
            role.setParentRoleId(parentRoleId);
            role.setDepartmentid(departmentid);
            //role.setFileinfo(null);
            
    	
            return role;
	    }
            finally{}
    }
	
	
	public List<Rolepermissions> parseRP(String rootPath) throws SAXException, IOException{
		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	      
		try {
			DocumentBuilder db= dbf.newDocumentBuilder();
			//Change2UTF.change(rootPath);  //change other coded file to UTF-8
		 	File file = new File(rootPath);
         
          Document doc = db.parse(file); 
          String id=parseNode("id",doc);
          NodeList resources= doc.getElementsByTagName("string");
          if (resources.getLength()!=0){
          	List<Rolepermissions> rps=new ArrayList<Rolepermissions>();
               for (int i=0;i<resources.getLength();i=i+1){
              	 Rolepermissions rolepermissions=new Rolepermissions();
   	    	     Element valueNode =(Element) resources.item(i);
   	    	     NodeList valueList=valueNode.getChildNodes();
   	    	     rolepermissions.setPermissionid(valueList.item(0).getNodeValue());
   	    	     rolepermissions.setRoleid(id);
   	    	     rps.add(rolepermissions);
   	       }
 		         return rps;
          }
         
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		 	
		 	
		 
		
	}
	public static void main(String[] args) throws Exception {
		String outFile="D:/eclipse for modeling 10.07.26/runtime-New_configuration/demo/组织视图/roles/7b1d4d8f-8afb-4921-9055-ec980b1c9ddf.xml";
		ParserRole test=new ParserRole();
		Role role=test.parse(outFile);
		
		
	}
}